﻿using System;
using System.Collections.Generic;
using System.Text;
using BCMS.DAL.IDAL;
using BCMS.Business.DataContract;
using MySql.Data.MySqlClient;
using System.Data;

namespace BCMS.DAL.MySQL
{
    public class Others : IOther
    {
        private MySqlConnection _internalConnection;
        private MySqlTransaction _internalADOTransaction = null;

        public void Open(string connString)
        {
            if (_internalConnection == null)
                _internalConnection = new MySqlConnection(connString);
            if (_internalConnection.State != ConnectionState.Open)
                _internalConnection.Open();
        }
        public void Close()
        {
            if (_internalConnection != null && _internalConnection.State != System.Data.ConnectionState.Closed)
                _internalConnection.Close();
        }

        private const string GET_ITEMLIST_BYTYPE = "SELECT * FROM others WHERE type=@type";

        private const string INSERT_OTHERITEM = "INSERT INTO other(type, name) VALUES (@type, @value)";

        #region IOther Members

        public List<OtherInfo> getOtherInfoListByType(string type)
        {
            MySqlDataReader rdr = null;
            List<OtherInfo> list = new List<OtherInfo>();
            try
            {
                MySqlParameter parm = new MySqlParameter("@TYPE", MySqlDbType.VarChar);

                rdr = MyHelper.ExecuteReaderSingleParm(_internalConnection, _internalADOTransaction, CommandType.Text, GET_ITEMLIST_BYTYPE, parm);

                if (rdr.HasRows)
                {
                    while (rdr.Read())
                        list.Add(ConvertToObject(rdr));
                    return list;
                }
                else
                    return null;
            }
            catch
            {
                throw;
            }
            finally
            {
                if (rdr != null)
                    rdr.Close();
            }
        }

        public int insertOtherInfo(string type, string name)
        {
            try
            {
                MySqlParameter[] parms = new MySqlParameter[] { 
                                        new MySqlParameter("@type", MySqlDbType.VarChar),
                                        new MySqlParameter("@name", MySqlDbType.VarChar)
                };

                return MyHelper.ExecuteNonQuery(_internalConnection, _internalADOTransaction, CommandType.Text, INSERT_OTHERITEM, parms);

            }
            catch
            {
                throw;
            }
        }

        public void updateTargetById(int id, string type, string name)
        {
            throw new NotImplementedException();
        }

        private OtherInfo ConvertToObject(MySqlDataReader rdr)
        {
            OtherInfo item = new OtherInfo();
            return item;
        }

        #endregion
    }
}
